package com.yixing.tech.wcs.controller;

import com.yixing.tech.common.base.*;
import com.yixing.tech.common.autoline.domain.AGVRasterRequest;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.entity.wcs.TaskVO;
import com.yixing.tech.common.entity.wcs.WCSTaskRequest;
import com.yixing.tech.wcs.domain.vo.RCSTaskRequestVO;
import com.yixing.tech.wcs.domain.vo.SubTaskVO;
import com.yixing.tech.wcs.domain.vo.WCSConfig;
import com.yixing.tech.common.entity.WCSTaskCallbackVO;
import com.yixing.tech.wcs.service.ITaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * WMS下发任务接口服务
 * */
@RestController
@CrossOrigin
@RequestMapping(value = "/tbea/ic/wcs/task")
@Slf4j
public class TaskController extends BaseControl {

    @Resource
    ITaskService taskService;

    @PostMapping("/genTask")
    public Rs<Object> genTask(@RequestBody TaskVO taskVO) throws BusinessException {
        log.info("genTask=======================================");
        log.info("请求参数:{}", taskVO);
        return Rs.ok(taskService.genTask(taskVO), "下发任务成功");
    }

    @PostMapping("/cancelTask")
    public Rs<Object> cancelTask(@RequestBody TaskVO taskVO) throws BusinessException {
        log.info("cancelTask=======================================");
        log.info("请求参数:{}", taskVO);
        return Rs.ok(taskService.cancelTask(taskVO), "取消任务成功");
    }

    @PostMapping("/cancelSubTask")
    public Rs<Object> cancelSubTask(@RequestBody SubTaskVO subTaskVO) throws BusinessException {
        log.info("cancelSubTask=======================================");
        log.info("请求参数:{}", subTaskVO);
        return Rs.ok(taskService.cancelSubTask(subTaskVO), "取消任务成功");
    }

    @GetMapping("/decomposeTaskByTaskCode/{taskCode}")
    public Rs<Object> decomposeTaskByTaskCode(@PathVariable String taskCode) throws BusinessException {
        taskService.decomposeTaskWithTaskCode(taskCode);
        return Rs.ok("ok");
    }

    @PostMapping("/decomposeTask")
    public Rs<Object> decomposeTask() throws BusinessException {
        Object obj = taskService.decomposeTask();
        return Rs.ok(obj);
    }

    @PostMapping("/allocateResourceForSubTask")
    public Rs<Object> allocateResourceForSubTask() throws BusinessException {
        taskService.allocateResourceForSubTask();
        return Rs.ok("ok");
    }

    @PostMapping("/startUpSubTask")
    public Rs<Object> startUpSubTask() throws BusinessException {
        taskService.startUpSubTask();
        return Rs.ok("ok");
    }

    @PostMapping("/initWCSConfig")
    public Rs<Object> initWCSConfig(@RequestBody WCSConfig config) {
        com.yixing.tech.common.autoline.task.WCSConfig.setConfig(config.getIp(), config.getPort());
        return Rs.ok(true, "初始化wcsConfig成功");
    }

    @PostMapping("/genWCSTask")
    public Rs<Object> genWCSTask(@RequestBody WCSTaskRequest request) throws BusinessException {
        log.info("genWCSTask=======================================");
        log.info("请求参数:{}", request);
        return Rs.ok(taskService.genWCSTask(request), "请求生成任务");
    }

    @PostMapping("/genWCSTaskMulti")
    public Rs<Object> genWCSTaskMulti(@RequestBody List<WCSTaskRequest> list) throws BusinessException {
        log.info("genWCSTaskMulti=======================================");
        log.info("请求参数:{}", list);
        return Rs.ok(taskService.genWCSTaskMulti(list), "请求生成任务");
    }

    @PostMapping("/genCTUTransportTask")
    public Rs<Object> genCTUTransportTask(@RequestBody TaskVO taskVO) throws BusinessException {
        log.info("genCTUTransportTask=======================================");
        log.info("请求参数:{}", taskVO);
        return Rs.ok(taskService.genCTUTransportTask(taskVO), "请求生成ctu任务");
    }

    @PostMapping("/genCTUOutboundTask")
    public Rs<Object> genCTUOutboundTask(@RequestBody RCSTaskRequestVO requestVO) throws BusinessException {
        log.info("genCTUOutboundTask=======================================");
        log.info("请求参数:{}", requestVO);
        return Rs.ok(taskService.genCTUOutboundTask(requestVO), "请求生成ctu出库任务");
    }

    @PostMapping("/genCTUInboundTask")
    public Rs<Object> genCTUInboundTask(@RequestBody RCSTaskRequestVO requestVO) throws BusinessException {
        log.info("genCTUInboundTask=======================================");
        log.info("请求参数:{}", requestVO);
        return Rs.ok(taskService.genCTUInboundTask(requestVO), "请求生成ctu入库任务");
    }

    @PostMapping("/boxApplyPass")
    public Rs<Object> boxApplyPass(@RequestBody RCSTaskRequestVO requestVO) throws BusinessException {
        log.info("boxApplyPass=======================================");
        log.info("请求参数:{}", requestVO);
        return Rs.ok(taskService.boxApplyPass(requestVO), "请求CTU放下/取出料箱");
    }

    @PostMapping("/openRaster")
    public Rs<Object> openRaster(@RequestBody AGVRasterRequest request) throws BusinessException {
        log.info("openRaster=======================================");
        log.info("请求参数:{}", request);
        return Rs.ok(taskService.requestOpenRaster(request), "请求打开光栅");
    }

    @PostMapping("/closeRaster")
    public Rs<Object> closeRaster(@RequestBody AGVRasterRequest request) throws BusinessException {
        log.info("closeRaster=======================================");
        log.info("请求参数:{}", request);
        return Rs.ok(taskService.requestCloseRaster(request), "请求关闭光栅");

    }

    @PostMapping("/releaseResource")
    public Rs<Object> releaseResource(@RequestBody WCSTaskCallbackVO request) throws BusinessException {
        log.info("releaseResource=======================================");
        log.info("请求参数:{}", request);
        taskService.releaseResource(request);
        return Rs.ok(true, "请求成功");
    }

    @PostMapping("/existRunningOutBoundTaskForCTU")
    public Rs<Object> existRunningOutBoundTaskForCTU() throws BusinessException {
        log.info("existRunningOutBoundTaskForCTU=======================================");
        return Rs.ok(taskService.existRunningOutBoundTaskForCTU(null), "请求成功");
    }
}
